﻿using HuiZe.education.Model;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace HuiZe.education.View
{
    /// <summary>
    /// PART_MySpace_Item.xaml 的交互逻辑
    /// </summary>
    public partial class PART_MySpace_Item : UserControl
    {
        public ReqGroupFileListDetialModel Data
        {
            get { return (ReqGroupFileListDetialModel)GetValue(DataProperty); }
            set { SetValue(DataProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Data.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DataProperty =
            DependencyProperty.Register("Data", typeof(ReqGroupFileListDetialModel), typeof(PART_MySpace_Item), new PropertyMetadata(null, (sender, e) =>
             {
                 PART_MySpace_Item _item = (PART_MySpace_Item)sender;
                 ReqGroupFileListDetialModel _data = null;
                 if (e.NewValue != null) _data = (ReqGroupFileListDetialModel)e.NewValue;

                 _item.Load(_data);
             }));

        public bool IsSelected { get; set; }
        public Action<int, bool> SelectedCallback { get; set; }
        public Action<ReqGroupFileListDetialModel> EditCallback { get; set; }
        public Action<ReqGroupFileListDetialModel> DeleteCallback { get; set; }

        public PART_MySpace_Item()
        {
            InitializeComponent();
            PART_Edit.Click += PART_Edit_Click;
            PART_Delete.Click += PART_Delete_Click;
            MouseEnter += PART_MySpace_Item_MouseEnter;
            MouseLeave += PART_MySpace_Item_MouseLeave;
            PART_IsSelect.Checked += PART_IsSelect_Checked;
            PART_IsSelect.Unchecked += PART_IsSelect_Unchecked;
            PART_FileIcon.MouseLeftButtonUp += PART_FileIcon_MouseLeftButtonUp;
        }

        void PART_FileIcon_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (e.Source is Image && e.OriginalSource is Image)
            {
                IsSelected = !IsSelected;
                PART_IsSelect.IsChecked = new bool?(IsSelected);
                e.Handled = true;
            }
        }

        public void Load(ReqGroupFileListDetialModel _data)
        {
            if (_data == null)
            {
                PART_FileName.Text = string.Empty;
                PART_FileIcon.Source = null;
            }
            else
            {
                PART_FileName.Text = _data.file_name;

                string _url = _data.file;
                switch (_data.ext_name)
                {
                    case "pdf": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/pdf.png"; break;
                    case "mp4": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/mp4.png"; break;
                    case "doc": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/doc.png"; break;
                    case "docx": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/doc.png"; break;
                    case "mp3": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/mp3.png"; break;
                    case "ppt": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/ppt.png"; break;
                    case "pptx": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/ppt.png"; break;
                    case "swf": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/swf.png"; break;
                    case "txt": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/txt.png"; break;
                    case "xls": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/xls.png"; break;
                    case "xlsx": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/xls.png"; break;
                    case "zip": _url = "pack://application:,,,/HuiZe.education;component/Ethan/Res/Images/MySpace/zip.png"; break;
                }
                if (!string.IsNullOrEmpty(_url))
                {
                    BitmapImage _bmp = new BitmapImage();
                    _bmp.CacheOption = BitmapCacheOption.OnLoad;
                    _bmp.BeginInit();
                    _bmp.UriSource = new Uri(_url, UriKind.RelativeOrAbsolute);
                    _bmp.EndInit();

                    if (_url.StartsWith("pack://"))
                        PART_FileIcon.Stretch = Stretch.Uniform;
                    else
                        PART_FileIcon.Stretch = Stretch.UniformToFill;

                    PART_FileIcon.Source = _bmp;
                }
            }
        }

        public void SetIsSelected(bool _is_selected)
        {
            PART_IsSelect.IsChecked = new bool?(_is_selected);
        }

        void PART_IsSelect_Unchecked(object sender, RoutedEventArgs e)
        {
            IsSelected = false;
            if (Data == null) return;

            if (SelectedCallback != null)
                SelectedCallback(Data.id, false);

            e.Handled = true;
        }

        void PART_IsSelect_Checked(object sender, RoutedEventArgs e)
        {
            IsSelected = true;
            if (Data == null) return;

            if (SelectedCallback != null)
                SelectedCallback(Data.id, true);

            e.Handled = true;
        }

        void PART_Delete_Click(object sender, RoutedEventArgs e)
        {
            if (DeleteCallback != null) DeleteCallback(Data);
        }

        void PART_Edit_Click(object sender, RoutedEventArgs e)
        {
            if (EditCallback != null) EditCallback(Data);
        }

        void PART_MySpace_Item_MouseLeave(object sender, MouseEventArgs e)
        {
            PART_Controller_Panel.Visibility = Visibility.Collapsed;
        }

        void PART_MySpace_Item_MouseEnter(object sender, MouseEventArgs e)
        {
            PART_Controller_Panel.Visibility = Visibility.Visible;
        }
    }
}
